import { ReactViewState, reactControl, ReactViewControl } from "pao-aop-client";

import React from "react";

import { addon, Ref, getObject, IPermissionService, IRoleService, Permission, log } from "pao-aop";
import UserEditorForm, { UserEditorFormValues } from "src/projects/industryInternetSign/components/user-editor";
import { IUserService } from "src/business/models/user";
import { message, Spin, Icon } from "antd";
import { ROUTE_PATH } from "../../app/util-tool";
/**
 * 组件：名称状态
 */
export interface UserEditorViewState extends ReactViewState {
    /** 用户信息数据 */
    base_data: any;
    /** 角色列表 */
    role_list: any[];
    /** 是否新增角色 */
    isadd?: boolean;
    /** 是否显示新增编辑企业按钮 */
    is_show?: boolean;
    /** 滚动条 */
    loading?: boolean;
}

/**
 * 组件：名称
 */
export class UserEditorView extends React.Component<UserEditorViewControl, UserEditorViewState> {
    constructor(props: any) {
        super(props);
        this.state = {
            base_data: [],
            role_list: [],
            isadd: false,
            is_show: false,
            loading: false
        };
    }

    /** 初始化服务 */
    userService?() {
        return getObject(this.props.userService_Fac!);
    }
    /** 权限服务 */
    permissionService?() {
        return getObject(this.props.permissionService_Fac!);
    }
    roleService?() {
        return getObject(this.props.roleService_Fac!);
    }
    componentDidMount() {
        this.setState({
            loading: true
        });
        this.roleService!()!.get_role_list!({}, 1, 10)!
            .then((data: any) => {
                console.info(data);
                if (data !== '无查询结果') {
                    this.setState({
                        role_list: data.result,
                        loading: false
                    });
                }

            })
            .catch(error => {
                this.setState({
                    loading: false
                });
                log("userEditorViewControl", error.message);
            });
        let id = this.props.match!.params.key;
        this.setState({
            isadd: !this.props.match!.params.key,
        });
        if (id) {
            this.setState({
                loading: true
            });
            this.userService!()!.get_user_by_id!(id)!
                .then((data: any) => {
                    this.setState({
                        base_data: data,
                        is_show: true,
                        loading: false
                    });
                })
                .catch(error => {
                    this.setState({
                        loading: false
                    });
                    log("userEditorViewControl", error.message);
                });
        }

    }
    /** 确定回调 */
    handleSubmit = (err: Error, values: UserEditorFormValues) => {
        this.setState({
            loading: true
        });
        console.log('submit:', values);
        let datas = {
            account: values['account'],
            comment: values['comment'] || '',
            id: values['id'],
            name: values['name'],
            role_id: values['role_id']
        };
        this.userService!()!.update!(datas)!
            .then((data: any) => {

                if (data === 'Success') {
                    message.info('保存成功！');
                    this.setState({
                        is_show: true,
                    });
                } else {
                    message.info(data);
                }
                this.setState({
                    loading: false
                });
            })
            .catch(error => {
                this.setState({
                    loading: false
                });
                log("userEditorViewControl", error.message);
            });
    }
    /** 返回回调 */
    handleBack = () => {
        console.log('back');
    }
    /** 新增编辑企业 */
    editor_enterprise = (err: Error, values: UserEditorFormValues) => {
        this.props.history!.push('/change-company-templateView/?key=' + values['account']);
    }
    reset_password = (id: string) => {
        this.setState({
            loading: true
        });
        this.userService!()!.reset_password!(id)!
            .then((data: any) => {
                this.setState({
                    loading: false
                });
                if (data === 'Success') {
                    message.info('重置密码成功');
                } else {
                    message.info('重置密码失败');
                }
            })
            .catch(error => {
                // debugger;
                this.setState({
                    loading: false
                });
                log("userEditorViewControl", error.message);
            });

    }
    returnBtn = () => {
        this.props.history!.push(ROUTE_PATH.userManage);
    }
    render() {
        const redeirect = this.props.isPermission ? this.props.isPermission!(this.props.permission!) : undefined;
        if (redeirect) {
            return redeirect;
        }
        const antIcon = <Icon type="loading" style={{ fontSize: 24 }} spin={true} />;
        return (
            (
                <Spin spinning={this.state.loading ? this.state.loading : false} indicator={antIcon} >
                    <UserEditorForm
                        base_data={this.state.base_data}
                        handleSubmit={this.handleSubmit}
                        handleBack={this.handleBack}
                        is_add={this.state.isadd}
                        role_list={this.state.role_list}
                        reset_password={this.reset_password}
                        return_btn={this.returnBtn}
                        is_show={this.state.is_show}
                        editor_enterprise={this.editor_enterprise}
                    />
                </Spin>
            )
        );
    }
}

/**
 * 控件：用户编辑控件
 * @description 控制用户编辑控件
 * @author
 */
@addon('UserEditorView', '设备编辑控件', '控制设备编辑控件')
@reactControl(UserEditorView, true)
export class UserEditorViewControl extends ReactViewControl {
    /** 用户服务 */
    public userService_Fac?: Ref<IUserService>;
    /** 权限服务接口 */
    public permissionService_Fac?: Ref<IPermissionService>;
    /** 角色服务 */
    public roleService_Fac?: Ref<IRoleService>;
    /** 视图权限 */
    public permission?: Permission;
    /** 权限判断方法 */
    public isPermission?: (permission: Permission) => React.ReactNode | undefined;

}